package com.apestech.framework.sql.lib;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.MultiDataBase;
import com.apestech.framework.util.StringFuncUtil;

public class InsertSql extends StringFuncUtil {
	
	private String sql;
	private String operSql;
	private String tabname;
	private boolean singleValue=false;
	private int fieldStart=-1;
	private int fieldEnd=-1;
	
	public InsertSql(String sql) throws Exception{
		this.sql=sql;
		this.operSql=sql.toLowerCase();
		String error=check();
		if(!"".equals(error)){
			throw new Exception(error);
		}
		getTabname();
		if(this.operSql.indexOf(" values ")>=0){
			this.singleValue=true;
		}
		getSqlstructure();
	}
	
	/**
	 * 返回空串代表无错，有错返回错误信息，不会判断全表操作
	 * @return
	 */
	public String check(){
		try {
			checkInsert();
		} catch (Exception e) {
			return e.getMessage();
		} 
		return "";
	}
	
	/**
	 * 创建SQL有效性检查
	 * @throws Exception
	 */
	private void checkInsert() throws Exception{
		int count=this.countString(" "+this.operSql, " insert ");
		if(count>1){
			throw new Exception(getMessage("SqlPaste_errorInsertSql","insert关键字只能有一个，实际有[({0})]个!",count));
		}
		if(this.operSql.indexOf(" into ")<0){
			throw new Exception(getMessage("SqlPaste_errorInsertSqlKey","请输入正确的插入SQL:缺少关键字[({0})]!","into"));
		}
		if(this.operSql.indexOf(" values ")<0 && this.operSql.indexOf(" select ")<0){
			throw new Exception(getMessage("SqlPaste_errorInsertSqlKey","请输入正确的插入SQL:缺少关键字[({0})]!","values / select"));
		}
	}
	
	/**
	 * 获取操作表名
	 */
	private void getTabname(){
		int start=this.operSql.indexOf(" into ")+6;
		int end=this.operSql.indexOf("(");
		this.tabname=this.sql.substring(start, end).trim();
	}
	
	/**
	 * 获取表名
	 * @return
	 */
	public String getTableName(){
		return this.tabname;
	}
	
	/**
	 * 是否为单值插入语句
	 * @return
	 */
	public boolean isSingleInsert(){
		return this.singleValue;
	}
	
	/**
	 * 获取SQL相关结构位置信息
	 */
	private void getSqlstructure(){
		this.fieldStart=this.operSql.indexOf("(")+1;
		this.fieldEnd=this.operSql.indexOf(")");
	}
	
	/**
	 * 获取主查询语句中的insert...(子串
	 * @return
	 * @throws Exception
	 */
	public String getInsertTab() throws Exception{
		return this.sql.substring(0, this.fieldStart-1);
	}
	
	/**
	 * 获取主查询语句中插入字段子串
	 * @return
	 * @throws Exception
	 */
	public String getInsertField() throws Exception{
		return this.sql.substring(this.fieldStart, this.fieldEnd);
	}
	
	/**
	 * 获取主查询语句中插入值
	 * @return
	 * @throws Exception
	 */
	public String getInsertValue() throws Exception{
		if(this.singleValue){
			String sub=this.sql.substring(this.fieldEnd+1, this.sql.length());
			return sub.substring(sub.indexOf("(")+1, sub.indexOf(")"));
		} else {
			return this.sql.substring(this.fieldEnd+1, this.sql.length());
		}
	}
	
	/**
	 * 获取查询SQL结构
	 * @return
	 * @throws Exception
	 */
	public JSONObject getSqlStructure() throws Exception{
		JSONObject map=new JSONObject();
		map.put("insert", this.getInsertTab());
		map.put("field", this.getInsertField());
		map.put("value", this.getInsertValue());
		return map;
	}
	
	//getVarPos(String text, String... params)

}
